package com.google.masai;

import java.io.File;
import java.io.FilePermission;
import java.io.IOException;
import java.util.ArrayList;

import com.dropbox.client.DropboxAPI;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;


// parameters progress result
public class DownloadAsync extends AsyncTask<DropBox, String, Integer> {

	private Activity  mActitivty;
	 
	
	
	private String dropboxFilePath;
	private String localFilePath;
	
	 
	
	public DownloadAsync (Activity activity, String dropboxFilePath, String localFilePath){
		
		
		this.dropboxFilePath = dropboxFilePath;
		this.localFilePath = localFilePath; 
		
		
		
		mActitivty = activity;
		
		
	}
	
	// Runs on main thread.
    @Override
    protected void onProgressUpdate(String... msgs) {
        if (msgs[0] != null) {
            showToast(msgs[0]);
        }
    }
	
	//
	protected Integer doInBackground(DropBox... params) {
		
		DropBox db = params[0];
		
		publishProgress("Starting Download Operation");
		 
		// Print log msg
		log("Dropbox path is " + dropboxFilePath + " and localfilepath=" + localFilePath); 
		
		File localFile = new File(localFilePath); 
		
		try {
			db.downloadDropboxFile(dropboxFilePath, localFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log(e.toString());
			
			return DropboxAPI.STATUS_FAILURE; 
		}
		
		return DropboxAPI.STATUS_SUCCESS;
		
		
		
    }
	
	
	@Override
	protected void onPostExecute(Integer result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		
		log("Download Completed");
		showToast("Download completed");
	
		return ; 
	}

	
	
	public void log(String msg){
		
		Log.d("DROPBOX", msg);
	}
	
	
	public void showToast(String msg) {
		Toast error = Toast.makeText(mActitivty, msg, Toast.LENGTH_LONG);
        error.show();
    }
	
	
}
